package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {

    @Autowired
    private IYddAdvimgService advimgService;

    @Autowired
    private IYddAdvposService advposService;

    /**
     * 分页查询广告图
     *
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advimgdesc, String advimgpos, int page, int limit) {

        Page<YddAdvimg> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(advimgdesc), YddAdvimg::getAdvimgdesc, advimgdesc);
        wrapper.like(StringUtils.isNotEmpty(advimgpos), YddAdvimg::getAdvimgpos, advimgpos);
        wrapper.orderByDesc(YddAdvimg::getId);
        advimgService.page(pageInfo, wrapper);

        List<YddAdvimgDto> yddAdvimgDtos = pageInfo.getRecords().stream().map(item -> {
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(item, yddAdvimgDto);
            //查询该广告位下有多少广告
            LambdaQueryWrapper<YddAdvpos> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(YddAdvpos::getId, item.getAdvimgpos());
            YddAdvpos yddAdvpos = advposService.getOne(wrapper1);
            System.out.println("yddAdvpos = " + yddAdvpos);

            yddAdvimgDto.setAdvposname(yddAdvpos.getAdvposname());

            return yddAdvimgDto;
        }).collect(Collectors.toList());

        //将数据封装为pageResult
        PageResult pageResult = PageResult.getResultByDtoPage(pageInfo,yddAdvimgDtos);

        return ResultData.ok("",pageResult);

    }


    /**
     * 修改时数据回显
     *
     * @param yddAdvimg
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(YddAdvimg yddAdvimg) {

        YddAdvimg yddAdvimg1 = advimgService.getById(yddAdvimg.getId());
        return ResultData.ok("",yddAdvimg1);

    }


    /**
     * 修改数据
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvimg yddAdvimg) {

        advimgService.updateById(yddAdvimg);
        return ResultData.ok("","");

    }

    /**
     * 新增数据
     *
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody YddAdvimg yddAdvimg) {

        advimgService.save(yddAdvimg);
        return ResultData.ok("","");

    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(@RequestParam List<Integer> id) {

        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(YddAdvimg::getId,id);
        List<YddAdvimg> yddAdvimgs = advimgService.list(wrapper);
        for (YddAdvimg yddAdvimg : yddAdvimgs) {
            if("1".equals(yddAdvimg.getDefaultData())){
                throw new BusinessException("系统默认数据无法删除!");
            }
        }


        advimgService.removeByIds(id);
        return ResultData.ok("","");

    }
}
